/**
 * @author hewei
 * @version 1.0
 * @description: 640. 求解方程
 * @date 2022/8/10 15:41
 */

public class SolveEquation {

    public static void main(String[] args) {
        SolveEquation solveEquation = new SolveEquation();
        System.out.println(solveEquation.solveEquation("0x=0"));
    }

    public String solveEquation(String equation) {
        String[] split = equation.split("=");
        int[] process1 = process(split[0]);
        int[] process2 = process(split[1]);
        int one = process1[0] - process2[0];
        int two = process2[1] - process1[1];
        if (one == 0) {
            if (two == 0) return "Infinite solutions";
            return "No solution";
        }
        int res = two / one;
        return "x=" + res;
    }

    public int[] process(String s) {
        int i = 0;
        boolean flag = false;
        int one = 0;
        int two = 0;
        while (i < s.length()) {
            int temp = 0;
            while (i < s.length() && Character.isDigit(s.charAt(i))) {
                temp = temp * 10 + s.charAt(i) - '0';
                ++i;
            }
            if (i == s.length() || s.charAt(i) == '-' || s.charAt(i) == '+') {
                two += flag ? -temp : temp;
            } else {
                if (i == 0 || !Character.isDigit(s.charAt(i - 1))) temp = 1;
                one += flag ? -temp : temp;
                ++i;
            }
            flag = i != s.length() && s.charAt(i) == '-';
            ++i;
        }
        return new int[]{one, two};
    }
}
